package com.ruoyi.easywenku.tool;

import cn.hutool.core.io.resource.ClassPathResource;
import cn.hutool.extra.qrcode.QrCodeUtil;
import org.springframework.util.ResourceUtils;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;


public class ImageUtil {




    public static BufferedImage overlapImage(String title,String text,String qc) {
        try {
            // 设置图片大小
            //设置图片大小
            //BufferedImage background = resizeImage(518,720, ImageIO.read(new File(backgroundPath)));
            //BufferedImage qrCode = resizeImage(80,80,ImageIO.read(new File(qrCodePath)));
            //BufferedImage gzh = resizeImage(80,80,ImageIO.read(new File(gzhPath)));


            //加头头
            BufferedImage top = ImageIO.read(new ClassPathResource("image/top.jpg").getStream());
            //加尾巴
            BufferedImage end = ImageIO.read(new ClassPathResource("image/end.jpg").getStream());
            //加文字
            BufferedImage textImage = text2Image(title, text);

            BufferedImage qrCode = QrCodeUtil.generate(qc, 116, 116);

            BufferedImage background = new BufferedImage(600, textImage.getHeight()+top.getHeight()+end.getHeight(), BufferedImage.TYPE_INT_RGB);
            Graphics2D g = background.createGraphics();
            g.setBackground(new Color(255,142,48));
            g.clearRect(0, 0, 600, 720);
            //在背景图片上图片
            g.drawImage(top, 0, 0, top.getWidth(), top.getHeight(), null);
            g.drawImage(textImage, (background.getWidth()-textImage.getWidth())/2, top.getHeight(), textImage.getWidth(), textImage.getHeight(), null);
            g.drawImage(end, 0, top.getHeight()+textImage.getHeight(), end.getWidth(), end.getHeight(), null);
            g.drawImage(qrCode, 285, background.getHeight()-135, qrCode.getWidth(), qrCode.getHeight(), null);
            //g.drawImage(gzh, 660, 920, gzh.getWidth(), gzh.getHeight(), null);


            g.dispose();

            return background;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static BufferedImage resizeImage(int x, int y, BufferedImage bfi) {
        BufferedImage bufferedImage = new BufferedImage(x, y, BufferedImage.TYPE_INT_RGB);
        bufferedImage.getGraphics().drawImage(bfi.getScaledInstance(x, y, Image.SCALE_SMOOTH), 0, 0, null);
        return bufferedImage;
    }

    public static BufferedImage text2Image(String title,String text) {
        try {



            int backgroundw = 518;
            int fontsSize = 16;
            int rowSpace = 10;
            int rowMargin = 20;

            int titleFontsSize = 34;
            int titleRowHigh = 40;
            int tRowSize = (backgroundw-rowMargin*2) / titleFontsSize;


            int rowHigh = rowSpace + fontsSize;
            int rowSize = (backgroundw-rowMargin*2) / fontsSize;
            int titleHigh = titleRowHigh * (title.length() / tRowSize + 1);

            int backgroundh = (text.length() / rowSize + 1) * rowHigh + rowSpace+titleHigh;

            BufferedImage image = new BufferedImage(backgroundw, backgroundh, BufferedImage.TYPE_INT_RGB);

            Graphics2D g = image.createGraphics();
            g.setBackground(Color.WHITE);
            g.clearRect(0, 0, backgroundw, backgroundh);
            g.setColor(Color.black);
            g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,RenderingHints.VALUE_TEXT_ANTIALIAS_LCD_HRGB);


            g.setFont(new Font("微软雅黑", Font.PLAIN, titleFontsSize));
            int i1 = titleFontsSize * title.length();
            int i2 = (backgroundw - i1)/2;
            if (rowMargin+i2>0){
                g.drawString(title, i2, titleFontsSize);
            }else {
                for (int i = 0; i  <= title.length() / tRowSize; i++) {
                    int last =(i + 1) * tRowSize;
                    if (last > title.length()) {
                        last = title.length();
                    }
                    g.drawString(title.substring(i * tRowSize, last), rowMargin, (i + 1) * titleRowHigh);

                }
            }

            g.setFont(new Font("微软雅黑", Font.PLAIN, fontsSize));

            for (int i = 0; i - 1 < text.length() / rowSize; i++) {
                int last = i * rowSize + rowSize;
                if (last > text.length()) {
                    last = text.length();
                }
                g.drawString(text.substring(i * rowSize, last), rowMargin, (i + 1) * rowHigh+(titleHigh));
            }
            g.dispose();
            return image;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
